#include "uart_shell.h"
#include <string.h>
#include "ble_lbs_c.h"
#include "nrf_log.h"
#include "app_clock.h"
#include "protocol_analysis.h"
#include "app_timer_process.h"
#include "drv_audio.h"
#include "user_speech_recognition.h"
#include "bluetooth_link.h"

uint8_t parse_shell_cmd(uint8_t *cmd,uint8_t cnt)
{
    clock_time_t time;
    clock_time_t* p_time = &time;
    uint8_t cmd_char;

    if(memcmp(cmd,"open curtain",cnt) == 0)
    {
        NRF_LOG_PRINTF("send open cmd ...\r\n");
        Open_Curtain_Command_Tx(100);
        //ble_Send_Buff();
    }
    else if(memcmp(cmd,"close curtain",cnt) == 0)
    {
        NRF_LOG_PRINTF("send close cmd ...\r\n");
        Close_Curtain_Command_Tx(100);
        //ble_Send_Buff();
    }
    else if(memcmp(cmd,"stop curtain",cnt) == 0)
    {
        NRF_LOG_PRINTF("send stop curtain cmd ...\r\n");
        Stop_Curtain_Command_Tx(100);
        //ble_Send_Buff();
    }
    else if(memcmp(cmd,"turnOn Lamp",cnt) == 0)
    {
        NRF_LOG_PRINTF("turnOn Lamp...\r\n");
        TurnOnLamp_Command_Tx(100);
    }
    else if(memcmp(cmd,"turnOff Lamp",cnt) == 0)
    {
        NRF_LOG_PRINTF("turnOff Lamp...\r\n");
        TurnOffLamp_Command_Tx(100);
    }
    else if(memcmp(cmd,"get time",cnt) == 0)
    {
        p_time  = Get_Clock_time();
        NRF_LOG_PRINTF("current time:\r\n");
        NRF_LOG_PRINTF("%d-%d-%d %d:%d:%d  week:%d \r\n",p_time->year,p_time->month,p_time->day,p_time->hour,p_time->minute,p_time->second,p_time->day_of_week);
    }
    else if(memcmp(cmd,"set time",8) == 0)
    {
        NRF_LOG_PRINTF("set time fmt <set time 20201203215230 4>\r\n",cmd);
        p_time->year        = (cmd[9] - '0') * 1000 + (cmd[10] - '0') * 100 + (cmd[11] - '0') * 10 + (cmd[12] - '0');
        p_time->month       = (cmd[13] - '0') * 10 + (cmd[14] - '0');
        p_time->day         = (cmd[15] - '0') * 10 + (cmd[16] - '0');
        p_time->hour        = (cmd[17] - '0') * 10 + (cmd[18] - '0');
        p_time->minute      = (cmd[19] - '0') * 10 + (cmd[20] - '0');
        p_time->second      = (cmd[21] - '0') * 10 + (cmd[22] - '0');
        p_time->day_of_week = (cmd[24] - '0');
        NRF_LOG_PRINTF("set time:");
        NRF_LOG_PRINTF("%d-%d-%d %d:%d:%d  week:%d \r\n",p_time->year,p_time->month,p_time->day,p_time->hour,p_time->minute,p_time->second,p_time->day_of_week);
        Set_Clock_time(p_time);
    }
    else if(memcmp(cmd,"set curtain1 time",cnt) == 0)
    {
        NRF_LOG_PRINTF("Set_Time\r\n");
        p_time  = Get_Clock_time();
        Set_Time_Command_Tx(p_time);
    }
    else if(memcmp(cmd,"dfu",8) == 0)
    {
        NRF_LOG_PRINTF("Start OTA\r\n");
        Enter_Dfu_Mode();
    }
    else if(memcmp(cmd,"Start read",10) == 0)
    {
        //app_timer_erase_start();
        //start_pcm_data(0);
        user_algo_asr_start(0);
    }
    // 训练 Weak Up 指令
    else if(memcmp(cmd,"imx Weak Up",11) == 0)
    {
        user_algo_asr_trainning_start(1);
    }
    // 训练 Open Curtain 指令
    else if(memcmp(cmd,"imx Open Curtain",16) == 0)
    {
        user_algo_asr_trainning_start(2);
    }
    // 训练 Close Curtain 指令
    else if(memcmp(cmd,"imx Close Curtain",17) == 0)
    {
        user_algo_asr_trainning_start(3);
    }
    // 训练 Open Light 指令
    else if(memcmp(cmd,"imx Open Light",14) == 0)
    {
        user_algo_asr_trainning_start(4);
    }
    // 训练 Close Light 指令
    else if(memcmp(cmd,"imx Close Light",15) == 0)
    {
        user_algo_asr_trainning_start(5);
    }
    else if(memcmp(cmd,"Scan device",11) == 0)
    {
        send_finded_device();
        scan_start();
    }
    else if(memcmp(cmd,"Device Binding",14) == 0)
    {
        cmd_char = cmd[14] - '0';
        set_connect_dev_index(cmd_char, 1);
        ble_connect(cmd_char);
    }
    else if(memcmp(cmd,"print audio",11) == 0)
    {
        print_audio_raw();
    }
}
